/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.City;
import cc.altius.model.HealthSpringCenter;
import cc.altius.model.MasterZone;
import cc.altius.service.CenterService;
import cc.altius.service.ZoneService;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author sagar
 */
@Controller
@RequestMapping("centers")
public class CenterController {

    @Autowired
    private CenterService centerService;
    @Autowired
    private ZoneService zoneService;

    @ModelAttribute("cityList")
    public List<City> populateCityList() {
        return this.zoneService.getCityList();
    }

    @ModelAttribute("masterZoneList")
    public List<MasterZone> populateMasterZoneList() {
        return this.zoneService.getMasterZoneList();
    }

    @RequestMapping(value = "/addCenters.htm", method = RequestMethod.GET)
    public String showCenterGetPage(ModelMap modelMap) {
        HealthSpringCenter center = new HealthSpringCenter();
        modelMap.addAttribute("center", center);
        return "centers/addCenters";
    }

    @RequestMapping(value = "/addCenters.htm", method = RequestMethod.POST)
    public String showCenterPostPage(@ModelAttribute("center") HealthSpringCenter center, ModelMap modelMap) {
        int centerId = this.centerService.addCenter(center);
        return "redirect:listCenters.htm?msg=Succesfully Added Healthspring Community Medical Center";
    }

    @RequestMapping(value = "/listCenters.htm")
    public String showListCenterGetPage(ModelMap modelMap, @RequestParam(value = "cityId", defaultValue = "0", required = true) int cityId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("centerList", centerService.getCenterList(cityId));
        map.put("cityId", cityId);
        modelMap.addAttribute("dataMap", map);
        return "centers/listCenters";
    }

    @RequestMapping(value = "/editCenters.htm", method = RequestMethod.GET)
    public String showEditCenterGetPage(@RequestParam(value = "centerId") int centerId, ModelMap modelMap) {
        HealthSpringCenter center = this.centerService.getCenterById(centerId);
        modelMap.addAttribute("center", center);
        return "centers/editCenters";
    }

    @RequestMapping(value = "/editCenters.htm", method = RequestMethod.POST)
    public String onSubmitPage(@ModelAttribute("center") HealthSpringCenter center, @RequestParam(value = "centerId", required = true) int centerId) {
    
        int updateCenterId = this.centerService.updateCenter(center, centerId);
        if (updateCenterId != 0) {
            return "redirect:listCenters.htm?msg=Succesfully Updated Center";
        } else {
            return "redirect:editCenters.htm?error=center not successfully updated";
        }

    }
}
